package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.CruxDevicePosition;
import com.ruoyi.system.domain.SysDanger;
import com.ruoyi.system.entity.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysDangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDangerServiceImpl implements ISysDangerService {

    @Autowired
    private SysDangerMapper sysDangerMapper;

    @Autowired
    private SysDangerPointMapper sysDangerPointMapper;

    @Autowired
    private SysGasPointMapper sysGasPointMapper;

    @Autowired
    private SysEnvironmentPointMapper sysEnvironmentPointMapper;

    @Autowired
    private CruxDevicePositionMapper cruxDevicePositionMapper;

    @Autowired
    private SysAnalysisUnitMapper sysAnalysisUnitMapper;

    @Autowired
    private PreventionHiddenRegisterMapper preventionHiddenRegisterMapper;

    @Override
    public void create(SysDanger sysDanger) {
        sysDangerMapper.create(sysDanger);
    }

    @Override
    public List<SysDanger> list(SysDanger sysDanger) {
        return sysDangerMapper.list(sysDanger);
    }

    @Override
    public void delete(Integer id) {
        sysDangerMapper.delete(id);
    }

    @Override
    public void update(SysDanger sysDanger) {
        sysDangerMapper.update(sysDanger);
    }

    @Override
    public List<SysDanger> listByLevel() {
        return sysDangerMapper.listByLevel();
    }

    @Override
    public List<Map<String, Object>> getGeoData() {
        List<Map<String, Object>> mapList = new ArrayList<>();
        List<SysDanger> list = sysDangerMapper.list(new SysDanger());
        for (SysDanger sysDanger : list) {
            String addressInfo = sysDanger.getAddressInfo();
            if (StringUtils.isEmpty(addressInfo)) {
                continue;
            }
            Map<String, Object> map = new HashMap<>();
            String dangerLevel = sysDanger.getRiskLevel();
            String s = colourLevel(dangerLevel);
            map.put("colour",s);
            map.put("index",addressInfo);
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public Map<String, Object> getDangerCount() {
        Map<String, Object> map = new HashMap<>();
        // 重大危险源统计
        long danger = sysDangerPointMapper.count(new SysDangerPoint());
        map.put("danger",danger);
        // 可燃气体统计
        long gas = sysGasPointMapper.count(new SysGasPoint());
        map.put("gas",gas);
        // 关键重大部位统计
        long crux = cruxDevicePositionMapper.count(new CruxDevicePosition());
        map.put("crux",crux);
        // 环境检测统计
        long env = sysEnvironmentPointMapper.count(new SysEnvironmentPoint());
        map.put("env",env);
        // 风险点统计
        List<Map<String,Integer>> list = sysAnalysisUnitMapper.countByLevel();
        map.put("dangerPoint",list);
        long count = sysAnalysisUnitMapper.count(new SysAnalysisUnit());
        map.put("dangerPointTotal",count);
        // 设备设施数量统计
        // 作业活动数量统计
        return map;
    }

    @Override
    public Map<String, Object> getHiddenCount() {
        Map<String, Object> map = new HashMap<>();
        // 根据级别分
        List<Map<String,Integer>> list = preventionHiddenRegisterMapper.countByLevel();
        map.put("level",list);
        long count = preventionHiddenRegisterMapper.count(new PreventionHiddenRegister());
        map.put("total",count);
        // 根据设备分
        List<Map<String, Integer>> equipment = preventionHiddenRegisterMapper.countByHiddenType("设备");
        map.put("equipment",equipment);
        // 根据操作分
        List<Map<String, Integer>> task = preventionHiddenRegisterMapper.countByHiddenType("安全");
        map.put("task",task);
        return map;
    }

    @Override
    public List<Map<String, Integer>> countByLevel() {
        return sysDangerMapper.countByLevel();
    }

    public String colourLevel(String level) {

        switch (level) {
            case "四级":
                return "rgba(0,0,255,0.5)";
            case "三级":
                return "rgba(255,255,0,0.5)";
            case "二级":
                return "rgba(255,97,0,0.5)";
            case "一级":
                return "rgba(255,0,0,0.5)";
        }
        return "rgba(255,255,255,0.3)";
    }
}
